package com.huilian.cloud.module.system.service.datascope;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.huilian.cloud.module.system.controller.admin.datascope.vo.*;
import com.huilian.cloud.module.system.dal.dataobject.datascope.DatascopeDO;
import com.huilian.cloud.framework.common.pojo.PageResult;
import com.huilian.cloud.framework.common.pojo.PageParam;
import com.huilian.cloud.framework.common.util.object.BeanUtils;

import com.huilian.cloud.module.system.dal.mysql.datascope.DatascopeMapper;

import static com.huilian.cloud.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.huilian.cloud.module.system.enums.ErrorCodeConstants.*;

/**
 * 数据权限 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class DatascopeServiceImpl implements DatascopeService {

    @Resource
    private DatascopeMapper datascopeMapper;

    @Override
    public Integer createDatascope(DatascopeSaveReqVO createReqVO) {
        // 插入
        DatascopeDO datascope = BeanUtils.toBean(createReqVO, DatascopeDO.class);
        datascopeMapper.insert(datascope);
        // 返回
        return datascope.getDataScopeId();
    }

    @Override
    public void updateDatascope(DatascopeSaveReqVO updateReqVO) {
        // 校验存在
        validateDatascopeExists(updateReqVO.getDataScopeId());
        // 更新
        DatascopeDO updateObj = BeanUtils.toBean(updateReqVO, DatascopeDO.class);
        datascopeMapper.updateById(updateObj);
    }

    @Override
    public void deleteDatascope(Integer id) {
        // 校验存在
        validateDatascopeExists(id);
        // 删除
        datascopeMapper.deleteById(id);
    }

    private void validateDatascopeExists(Integer id) {
        if (datascopeMapper.selectById(id) == null) {
            throw exception(DATASCOPE_NOT_EXISTS);
        }
    }

    @Override
    public DatascopeDO getDatascope(Integer id) {
        return datascopeMapper.selectById(id);
    }

    @Override
    public PageResult<DatascopeDO> getDatascopePage(DatascopePageReqVO pageReqVO) {
        return datascopeMapper.selectPage(pageReqVO);
    }

}